package com;


import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import model.Instance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.gcm.service.PersistanceService;

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/hello")
public class RestServer {

	@Autowired
	PersistanceService persistanceService;
	
	
	
  public RestServer() {
		super();
		System.out.println("rest olustu");
	}
  
  @Context
  private ServletContext context; 

// This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

  
    @GET
	@Path("/{param}/{param2}")
	public Response printMessage(@PathParam("param") String msisdn,@PathParam("param2") String regID) {
    	ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);;
		String result = "Restful example : " + msisdn;
		persistanceService = (PersistanceService)applicationContext.getBean("persistanceService");
		
		
		Instance instance = new Instance(msisdn,regID,"");
		persistanceService.add(instance);
		
		System.out.println("size="+persistanceService.getInstanceList().size());
		return Response.status(200).entity(result).build();
	

	}
    
    @GET
   	@Path("/{param}/{param2}/{param3}/{param4}/{param5}")
   	public Response printMessage(@PathParam("param") String msisdn,@PathParam("param2") String regID
   			,@PathParam("param3") String lat,@PathParam("param4") String lng,@PathParam("param5") String company) {
       	ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);;
   		String result = "Restful example : " + msisdn;
   		persistanceService = (PersistanceService)applicationContext.getBean("persistanceService");
   		
   		
   		
   		Instance instance = new Instance(msisdn,regID,company);
   		persistanceService.add(instance);
   		
   		Util.Util.fillObject(instance, lat, lng);
   		
   		System.out.println("size="+persistanceService.getInstanceList().size());
   		return Response.status(200).entity(result).build();
   	

   	}
} 